package com.ihighest.app.infox.activity;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

import com.ihighest.app.common.base.AbstractActivity;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.infox.R;
import com.lidroid.xutils.ViewUtils;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;

@SuppressWarnings("deprecation")
public class MainActivity extends AbstractActivity implements OnCheckedChangeListener {

	Intent indexIntent, searchIntent, gClassIntent, shopcarIntent, pCenterIntent;
	public static RadioButton rb1, rb2, rb3, rb4, rb5;
	String authTabId = "TAB_4,TAB_5";
	TabHost tabHost;
	LocalActivityManager mLocalActivityManager;

	// private PushAgent mPushAgent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_maintext);
		tabHost = (TabHost) findViewById(android.R.id.tabhost);

		mLocalActivityManager = new LocalActivityManager(this, false);
		mLocalActivityManager.dispatchCreate(savedInstanceState);
		tabHost.setup(mLocalActivityManager);

		ViewUtils.inject(this);
		initUI();
		rb1.setChecked(true);
		// mPushAgent = PushAgent.getInstance(this);
		// 版本检测
		UmengUpdateAgent.update(this);
		// 反馈回复检测
		FeedbackAgent agent = new FeedbackAgent(this);
		agent.sync();
	}

	/**
	 * 初始化界面元素
	 */
	void initUI() {
		indexIntent = new Intent(this, InformationActivity.class);
		searchIntent = new Intent(this, AgriculturalTechnologyActivity.class);
		gClassIntent = new Intent(this, CircleActivity.class);
		shopcarIntent = new Intent(this, FindActivity.class);
		pCenterIntent = new Intent(this, PersonalActivity.class);

		rb1 = (RadioButton) findViewById(R.id.radioButton1);
		rb1.setOnCheckedChangeListener(this);
		rb2 = (RadioButton) findViewById(R.id.radioButton2);
		rb2.setOnCheckedChangeListener(this);
		rb3 = (RadioButton) findViewById(R.id.radioButton3);
		rb3.setOnCheckedChangeListener(this);
		rb4 = (RadioButton) findViewById(R.id.radioButton4);
		rb4.setOnCheckedChangeListener(this);
		rb5 = (RadioButton) findViewById(R.id.radioButton5);
		rb5.setOnCheckedChangeListener(this);

		tabHost.addTab(buildTabSpec("TAB_1", R.string.infox_main_zixun, R.drawable.ic_launcher, this.indexIntent));

		tabHost.addTab(buildTabSpec("TAB_2", R.string.infox_main_nongji, R.drawable.ic_launcher, this.searchIntent));

		tabHost.addTab(buildTabSpec("TAB_3", R.string.infox_main_find, R.drawable.ic_launcher, this.gClassIntent));

		tabHost.addTab(buildTabSpec("TAB_4", R.string.infox_main_quanzi, R.drawable.ic_launcher, this.shopcarIntent));

		tabHost.addTab(buildTabSpec("TAB_5", R.string.infox_main_mian, R.drawable.ic_launcher, this.pCenterIntent));
	}

	TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
		return tabHost.newTabSpec(tag).setIndicator(getString(resLabel), getResources().getDrawable(resIcon)).setContent(content);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radioButton1:
				tabHost.setCurrentTabByTag("TAB_1");
				break;
			case R.id.radioButton2:
				tabHost.setCurrentTabByTag("TAB_2");
				break;
			case R.id.radioButton3:
				tabHost.setCurrentTabByTag("TAB_3");
				break;
			case R.id.radioButton4:
				tabHost.setCurrentTabByTag("TAB_4");
				break;
			case R.id.radioButton5:
				tabHost.setCurrentTabByTag("TAB_5");
				break;
			}
		}
	}

	@Override
	public void onResume() {
		mLocalActivityManager.dispatchResume();
		super.onResume();
	}

	@Override
	public void onPause() {
		mLocalActivityManager.dispatchPause(isFinishing());
		super.onPause();
	}

}
